--
-- Author: shunguo.chen
-- Date: 2017-03-27 14:38:27
--
-- BagSellView
--

local NumInputView = require("app.widget.NumInputView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BagSellUi

function ClassRef:init()
	self._ui.Button_quxiao:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)
	self._ui.Button_queren:addClickEventListener(function()
			executeFunc(self._onOperate, "sellItem", { {self._data.uid}, {self.m_sellNum} } )
			executeFunc(self._onCloseView)
		end)

	self._ui.Button_right:addClickEventListener(function() -- 增加个数
			self:refreshContent( self.m_sellNum + 1 )
		end)
	self._ui.Button_left:addClickEventListener(function() -- 减少个数
			self:refreshContent( self.m_sellNum - 1 )
		end)

	self.m_sellNum = 1 -- 初始化出售的个数
	self._maxNum = self._data.amount -- 堆叠上限

	self._itemName = self._ui["group_shenqing/group_sell/group/tlt/name"] -- 物品名字
	self._itemSellNum = self._ui["group_shenqing/group_sell/group/tlt_num/num"] -- 出售的个数
	self._itemUnitPrice = self._ui["group_shenqing/group_sell/group/sell/num"] -- 单价
	self._itemTotalPrice = self._ui["group_shenqing/group_sell/group/all_sell/num"] -- 总价
	self._itemSellBtn = self._ui["group_shenqing/group_sell/group/tlt_num/button_num"] -- 出售个数操作按钮
	self._itemSellBtn:addClickEventListener(function()
			self:openNumPanel()
		end)

	self:_initStaticContent()
end

function ClassRef:_initStaticContent()
	self._itemName:setString( self._data:getName() )

	if 1 == self._data.quality then
		self._itemName:setTextColor( cc.c4b(255,255,255,255) )
	else
		self._itemName:setTextColor( Res.medal_name_color[ self._data.quality - 1 ] )
	end

	self._itemSellNum:setString( self.m_sellNum )

	self._itemUnitPrice:setString( self._data:getSellPrice() )
	self._itemTotalPrice:setString( self.m_sellNum * tonumber( self._data:getSellPrice() ) )
end

function ClassRef:refreshContent( sellCount )
	if sellCount <= 1 then
		sellCount  = 1
	end
	if sellCount >= tonumber( self._data.amount ) then
		app:popMessage( "pluserror" )
		sellCount = self._data.amount
	end

	self.m_sellNum = sellCount

	self._itemSellNum:setString( self.m_sellNum )
	self._itemTotalPrice:setString( self.m_sellNum * tonumber( self._data:getSellPrice() ) )
end

function ClassRef:openNumPanel()
	local size = self._itemSellBtn:getContentSize()
	local pos = self._itemSellBtn:convertToWorldSpace(cc.p(size.width/2, size.height))
	pos.x = pos.x - 200
	local inputView = NumInputView.new(pos, 0, self._maxNum, 2, function(v)
			if v > 0 then
				self:refreshContent( v )
			end
		end)
    self:addChild(inputView)
end

return ClassRef
